//
//  LZComplainViewController.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/13.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZComplainViewController.h"
#import <IQKeyboardManager.h>

 static const NSInteger Max_Num_TextView = 200;
@interface LZComplainViewController ()<UITextViewDelegate>
/** textView */
@property (nonatomic ,strong) UITextView *complainTextView;
/** 发送按钮 */
@property (nonatomic ,strong) UIButton *sendBtn;

@end

@implementation LZComplainViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].enable = NO;
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.view resignFirstResponder];
    [IQKeyboardManager sharedManager].enable = YES;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"建议反馈";
    self.view.backgroundColor = KHColorFromRGB(0xf8f8f8);
    [self createTextView];
    [self createSendButton];
    
}

// 创建textView
- (void)createTextView {
    _complainTextView = [[UITextView alloc] init];
    _complainTextView.editable = YES;
    _complainTextView.delegate = self;
    _complainTextView.font = NEWFONT(15);
    _complainTextView.tintColor = KHGreenColor;
    _complainTextView.textColor = KHBlackColor;
    _complainTextView.backgroundColor = KHColorFromRGB(0xffffff);
    _complainTextView.delegate = self;
    [self.view addSubview:_complainTextView];
    [_complainTextView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.sizeOffset(CGSizeMake(SCREEN_Width, 250));
//        make.top.equalTo(self.view.mas_top).offset(10);
//        make.centerX.equalTo(self.view);
        make.width.offset(SCREEN_Width);
        make.height.offset(250*ysIZE);
        make.top.equalTo(self.view.mas_top).offset(10*ysIZE);
        make.centerX.equalTo(self.view);
    }];
    
    // 占位文字
    _placeHolderLabel = [[UILabel alloc] init];
    _placeHolderLabel.text = @"写下对我们平台的意见或建议吧~\n最多200个字哦！";
    _placeHolderLabel.enabled = NO;//lable必须设置为不可用
    _placeHolderLabel.numberOfLines = 0;
    _placeHolderLabel.backgroundColor = [UIColor clearColor];
    [_complainTextView addSubview:_placeHolderLabel];
    [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_complainTextView.mas_top).offset(10*ysIZE);
        make.left.equalTo(_complainTextView.mas_left).offset(10*xsIZE);
    }];
    
}

#pragma mark - 当textField布局发生改变的时候调用
- (void)textViewDidChange:(UITextView *)textView {
    
    //    对占位符的显示和隐藏做判断
    if (self.complainTextView.text.length == 0) {
        self.placeHolderLabel.text =  @"写下对我们平台的意见或建议吧~\n最多200字哦！";
    }else {
        self.placeHolderLabel.text = @"";
    }
    
    // 如果超过200
    if (self.complainTextView.text.length > Max_Num_TextView) {
        
        //对超出部分进行裁剪
        self.complainTextView.text = [self.complainTextView.text substringToIndex:Max_Num_TextView];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"最多只能输入200个字!请您合理安排内容!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *comAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alertController addAction:comAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


// 发送按钮
- (void)createSendButton {
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn setBackgroundColor:KHColorFromRGB(0x4bb92c)];
        [_sendBtn setTitleColor:KHColorFromRGB(0xffffff) forState:UIControlStateNormal];
        _sendBtn.layer.cornerRadius = 5;
        _sendBtn.layer.masksToBounds = YES;
        _sendBtn.titleLabel.font = NEWFONT(18);
        _sendBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_sendBtn];
        [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20*xsIZE);
            make.right.equalTo(self.view).offset(-20*xsIZE);
            make.top.equalTo(_complainTextView.mas_bottom).offset(25*ysIZE);
            make.height.offset(45*ysIZE);
        }];
        
        [_sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}

- (void)sendBtnClick {
    
    if (_complainTextView.text.length == 0) {
        [YJProgressHUD showMessage:@"内容不可以为空哦~" inView:self.view];
        return;
    }
    
    if (self.complainTextView.text.length >200) {
        [YJProgressHUD showMessage:@"超过200字了哦~" inView:self.view];
        return;
    }
    NSString * comText = [_complainTextView.text stringByReplacingOccurrencesOfString:@"\"" withString:@"”" ];
    
    NSDictionary *paramas = @{@"uid":[LZUserInfo shareInfo].uid,@"content":comText};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Personage/feedback" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"发送成功" inView:self.view];
            [self back];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"发送失败" inView:self.view];
    }];
    
}

// 返回上个界面
- (void)back{
    _complainTextView.text = @"";
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    });
    
}



@end
